package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 添加菜品
     * @param dto
     * @return
     */
    @PostMapping
    public R insert(@RequestBody DishDto dto) {
        dishService.addDish(dto);
        return R.success("添加成功");
    }

    @GetMapping("page")
    public R page(Integer page,Integer pageSize,String name){
        Page<Dish> dishPage = new Page<>(page,pageSize);
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper.orderByAsc("sort");
        dishService.page(dishPage, queryWrapper);

        Page<DishDto> dishDtoPage = new Page<>();
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<DishDto> records = new ArrayList<>();
        for (Dish dish : dishPage.getRecords()) {
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(dish,dto);
            Category category = categoryService.getById(dto.getCategoryId());
            dto.setCategoryName(category.getName());
            records.add(dto);
        }
        dishDtoPage.setRecords(records);
        return R.success(dishDtoPage);
    }

    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        Dish dish = dishService.getById(id);

        DishDto dto = new DishDto();
        BeanUtils.copyProperties(dish,dto);

        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id",id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dto.setFlavors(list);
        return R.success(dto);
    }

    @PutMapping
    public R updateById(@RequestBody DishDto dto){
        dishService.updateByDishId(dto);
        return R.success("查询成功");
    }

    @DeleteMapping
    public R deleteByIds(Long[] ids){
        dishService.removeByIds(Arrays.asList(ids));
        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dish_id",Arrays.asList(ids));
        dishFlavorService.remove(queryWrapper);
        return R.success("删除成功");
    }

    @PostMapping("status/{status}")
    public R updateStatus(@PathVariable Integer status,Long[] ids){
        Dish dish = new Dish();
        dish.setStatus(status);
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",Arrays.asList(ids));
        dishService.update(dish,queryWrapper);
        return R.success("更改成功");
    }

    @GetMapping("list")
    public R<List<DishDto>> list(Dish dish) {
        //构造查询条件
        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq(dish.getCategoryId() != null, "category_id", dish.getCategoryId());
        queryWrapper1.eq(StringUtils.isNotBlank(dish.getName()), "name", dish.getName());
        queryWrapper1.eq("status", 1);
        queryWrapper1.orderByAsc("sort").orderByAsc("update_time");
        //查询列表
        List<Dish> dishList = dishService.list(queryWrapper1);

        //将每个Dish对象转换为DishDto，放入dishDtoList集合中，响应
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish item : dishList) {
            DishDto dishDto = new DishDto();
            //基本属性拷贝
            BeanUtils.copyProperties(item, dishDto);
            //根据菜品id查询对应的口味集合
            QueryWrapper<DishFlavor> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("dish_id", item.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper2);
            //设置菜品口味的数据
            dishDto.setFlavors(dishFlavorList);
            dishDtoList.add(dishDto);
        }

        return R.success(dishDtoList);
    }

}
